﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BISPOS.Repository;
using System.Linq;
namespace BISPOS
{
    public partial class EditProductUserControl : DevExpress.XtraEditors.XtraUserControl
    {
        ProductRepository productRepoitory = new ProductRepository();
        List<Product> allProduct= new List<Product>();
        public EditProductUserControl()
        {
            InitializeComponent();
            this.searchTextEdit.Focus();
            
        }
        public void AddProduct(Product product)
        {
            this.groupControl2.Controls.Clear();
            ProductDetail productDetailsUserControl = new ProductDetail(product,this);
            this.groupControl2.Controls.Add(productDetailsUserControl);
            searchTextEdit.ResetText();
            searchTextEdit.Focus();

        }
        private void searchTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    this.groupControl2.Controls.Clear();
                    Product product = productRepoitory.Get(searchTextEdit.Text);
                    if (product != null)
                    {
                        ProductDetail productDetailsUserControl = new ProductDetail(productRepoitory.Get(searchTextEdit.Text),this);


                        this.groupControl2.Controls.Add(productDetailsUserControl);
                    }
                    else
                    {
                        MessageBox.Show("ไม่พบสินค้า");
                    }

                    searchTextEdit.ResetText();

                }
                catch 
                { 
                    MessageBox.Show("ไม่พบสินค้า");
                    searchTextEdit.ResetText();
                }
            }
            if (e.KeyCode == Keys.F1)
            {
                EditProduct_SelectProductForm selectProduct = new EditProduct_SelectProductForm(this, allProduct);
                selectProduct.Show();
            }
        }

        private void searchTextEdit_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void EditProductUserControl_Load(object sender, EventArgs e)
        {
            int all = productRepoitory.AllProducts().Count();
            decimal run = 0;
            foreach (var p in productRepoitory.AllProducts())
            {
                allProduct.Add(p);
                groupControl1.Text = "ค้นหาสินค้า";
            }
        }
    }
}
